<?php
//
class UsuariosController extends AppController {
	var $name = 'Usuarios';

	function escolar_index() {
		$this->Usuario->recursive = 0;
		$this->set('usuarios', $this->paginate());
	}

	function universitario_index() {
		$this->Usuario->recursive = 0;
		$this->set('usuarios', $this->paginate());
	}

        function logout(){
            $this->redirect($this->Auth->logout());            
        }

         function login(){
                if($this->Auth->user()){
                	$usuario = $this->Usuario->find('first', 
                                array(	'conditions'	=>
                                    array('Usuario.id' => $this->Auth->user('id')
                                            )
      					)
                		);

                	switch($usuario['Usuario']['tipo']){
                		case "Administrador":
                			$this->redirect(array('admin' => true, 'action' => 'index'));
                			break;
                		case "Escolar":
                			$this->redirect(array('escolar' => true, 'action' => 'index'));
                			break;
                		case "Universitario":
                			$this->redirect(array('universitario' => true, 'action' => 'index'));
                			break;
                	}

                }

        }



        function escolar_view($id = null) {

		if ((!$id) OR ($id != $this->Auth->user('id'))) {
			$this->Session->setFlash(__('Usuario inválido', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('usuario', $this->Usuario->read(null, $id));
	}

        function universitario_view($id = null) {

        if ((!$id) OR ($id != $this->Auth->user('id'))) {
			$this->Session->setFlash(__('Usuario inválido', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('usuario', $this->Usuario->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Usuario->create();
			if ($this->Usuario->save($this->data)) {
				$this->Session->setFlash(__('The usuario has been saved', true));
				$this->redirect(array('controller' => 'pages', 'action'=>'home'));
                                $this->Email->from = 'PORTAL REDU <escolares@clasesredu.cl>';
                                $this->Email->to = 'REDU <escolares@clasesredu.cl>';
                                $this->Email->subject = 'Solicitud de ayudantia';
			} else {
				$this->Session->setFlash(__('The usuario could not be saved. Please, try again.', true));
			}
		}
                $comunas = $this->Usuario->Comuna->find('list', array('order'=>'nombre ASC'));
		$this->set(compact('comunas'));
	}

	function escolar_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid usuario', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Usuario->save($this->data)) {
				$this->Session->setFlash(__('The usuario has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The usuario could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Usuario->read(null, $id);
		}
		$comunas = $this->Usuario->Comuna->find('list', array('order'=>'nombre ASC'));
		$this->set(compact('comunas'));
	}

	function universitario_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid usuario', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Usuario->save($this->data)) {
				$this->Session->setFlash(__('The usuario has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The usuario could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Usuario->read(null, $id);
		}
		$comunas = $this->Usuario->Comuna->find('list', array('order'=>'nombre ASC'));
		$this->set(compact('comunas'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for usuario', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Usuario->delete($id)) {
			$this->Session->setFlash(__('Usuario deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Usuario was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	//Funciones Admin
	function admin_index() {
		$this->Usuario->recursive = 0;
		$this->set('usuarios', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid usuario', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('usuario', $this->Usuario->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Usuario->create();
			if ($this->Usuario->save($this->data)) {
				$this->Session->setFlash(__('The usuario has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The usuario could not be saved. Please, try again.', true));
			}
		}
		$comunas = $this->Usuario->Comuna->find('list', array( "order" => "nombre ASC" ));
		$this->set(compact('comunas'));
                
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid usuario', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Usuario->save($this->data)) {
				$this->Session->setFlash(__('The usuario has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The usuario could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Usuario->read(null, $id);
		}
		$comunas = $this->Usuario->Comuna->find('list', array('order'=>'nombre ASC'));
		$this->set(compact('comunas'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for usuario', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Usuario->delete($id)) {
			$this->Session->setFlash(__('Usuario deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Usuario was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	//Funciones Cliente
	function cliente_view() {

		$datos_usuario = $this->Usuario->find('first', array(	'conditions'	=> array('Usuario.id' => $this->Auth->user('id')
                																			)
                													)
                									);
		$this->set('datos_usuario', $datos_usuario);

	}
	function cliente_solicitar_ayudantia() {


	}

	function cliente_asignaturas_disponibles(){

	}
}
?>